import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.BoxLayout;
import javax.swing.ButtonGroup;
import javax.swing.JPanel;
import javax.swing.JLabel;
import javax.swing.JTextField;
import java.awt.FlowLayout;
import java.awt.Component;
import javax.swing.Box;
import javax.swing.JOptionPane;
import javax.swing.SwingConstants;
import javax.swing.JRadioButton;
import javax.swing.JButton;
import java.awt.Dimension;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;

/**
 * This class represents the player configuration window that appears when a new game is started,
 * and allows assignment of name, selection of difficulty, and allocation of skill points. Data
 * collected from this class is used to create a new Player object.
 * @author Stanley Ayzenberg, Araya Zaesim, Michelle Partogi, Adam Perez
 * @version 1.0
 */
public class ConfigWindow {

	private JFrame frame;
	private JPanel panel;
	private JLabel lblPlayerName;
	private JTextField nameField;
	private Component horizontalGlue;
	private Box verticalBox;
	private Box horizontalBox;
	private Box horizontalBox_1;
	private JLabel label;
	private Component horizontalGlue_1;
	private JTextField textField_1;
	private Box horizontalBox_2;
	private JLabel lblDifficulty;
	private JRadioButton rdbtnEasy;
	private JRadioButton rdbtnBeginner;
	private JRadioButton rdbtnNormal;
	private JRadioButton rdbtnHard;
	private JRadioButton rdbtnImpossible;
	private Component horizontalGlue_2;
	private JLabel lblSkills;
	private Box horizontalBox_3;
	private JLabel lblPilot;
	private Component horizontalGlue_4;
	private Box horizontalBox_4;
	private JLabel pilotPointsLabel;
	private JButton button;
	private JButton button_1;
	private Box horizontalBox_5;
	private JLabel lblEngineer;
	private JButton button_2;
	private JLabel engineerPointsLabel;
	private JButton button_3;
	private Box horizontalBox_6;
	private JLabel lblTrader;
	private JButton button_4;
	private JLabel traderPointsLabel;
	private JButton button_5;
	private Box horizontalBox_7;
	private JLabel lblFighter;
	private JButton button_6;
	private JLabel fighterPointsLabel;
	private JButton button_7;
	private Component horizontalGlue_3;
	private Component horizontalGlue_5;
	private Component horizontalGlue_6;
	private Component horizontalGlue_7;
	private Component rigidArea;
	private Component rigidArea_1;
	private Component rigidArea_2;
	private Component rigidArea_3;
	private JLabel skillPointLabels;
	private JButton btnMakeCharacter;
	private String playerName;
	private int skillPoints;
	private int pilotPoints;
	private int fighterPoints;
	private int traderPoints;
	private int engineerPoints;
	private Player player;
	private SpaceTraderGui gui;

		/**
	 * Create the application.
	 */
	public ConfigWindow(SpaceTraderGui gui) {
		this.gui = gui;
		initialize();
		this.frame.setVisible(true);
	}

	/**
	 * Initialize the contents of the frame.
	 */
	private void initialize() {
		frame = new JFrame();
		frame.setBounds(100, 100, 450, 300);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.getContentPane().setLayout(new FlowLayout(FlowLayout.LEFT, 5, 5));
		
		/* Initial allocation of skill points (16 to use) */
		skillPoints = 16;
		pilotPoints = 0;
		fighterPoints = 0;
		traderPoints = 0;
		engineerPoints = 0;
		
		panel = new JPanel();
		frame.getContentPane().add(panel);
		panel.setLayout(new BoxLayout(panel, BoxLayout.X_AXIS));
		
		verticalBox = Box.createVerticalBox();
		panel.add(verticalBox);
		
		
		label = new JLabel("Player Name");
		
		horizontalGlue_1 = Box.createHorizontalGlue();
		
		textField_1 = new JTextField();
		textField_1.setColumns(10);
		
		horizontalBox = Box.createHorizontalBox();
		verticalBox.add(horizontalBox);
		
		lblPlayerName = new JLabel("Name");
		horizontalBox.add(lblPlayerName);
		
		horizontalGlue = Box.createHorizontalGlue();
		horizontalBox.add(horizontalGlue);
		
		nameField = new JTextField();
		nameField.addKeyListener(new KeyAdapter() {
			@Override
			public void keyTyped(KeyEvent e) {
				if(nameField.getText().isEmpty() == false && skillPoints == 0) btnMakeCharacter.setEnabled(true);
			}
		});
		horizontalBox.add(nameField);
		nameField.setColumns(10);
		
		horizontalBox_1 = Box.createHorizontalBox();
		verticalBox.add(horizontalBox_1);
		
		lblDifficulty = new JLabel("Difficulty");
		lblDifficulty.setHorizontalAlignment(SwingConstants.CENTER);
		horizontalBox_1.add(lblDifficulty);
		
		horizontalGlue_2 = Box.createHorizontalGlue();
		horizontalBox_1.add(horizontalGlue_2);
		
		rdbtnBeginner = new JRadioButton("Beginner");
		horizontalBox_1.add(rdbtnBeginner);
		
		rdbtnEasy = new JRadioButton("Easy");
		horizontalBox_1.add(rdbtnEasy);
		
		rdbtnNormal = new JRadioButton("Normal");
		rdbtnNormal.setSelected(true);
		horizontalBox_1.add(rdbtnNormal);
		
		rdbtnHard = new JRadioButton("Hard");
		horizontalBox_1.add(rdbtnHard);
		
		rdbtnImpossible = new JRadioButton("Impossible");
		horizontalBox_1.add(rdbtnImpossible);
		ButtonGroup group = new ButtonGroup();
		group.add(rdbtnBeginner);
		group.add(rdbtnEasy);
		group.add(rdbtnNormal);
		group.add(rdbtnHard);
		group.add(rdbtnImpossible);
		
		horizontalBox_4 = Box.createHorizontalBox();
		verticalBox.add(horizontalBox_4);
		
		lblSkills = new JLabel("Skill Points: ");
		horizontalBox_4.add(lblSkills);
		
		skillPointLabels = new JLabel("" + skillPoints);
		horizontalBox_4.add(skillPointLabels);
		
		horizontalGlue_4 = Box.createHorizontalGlue();
		horizontalBox_4.add(horizontalGlue_4);
		
		horizontalBox_3 = Box.createHorizontalBox();
		verticalBox.add(horizontalBox_3);
		
		lblPilot = new JLabel("Pilot");
		horizontalBox_3.add(lblPilot);
		
		horizontalGlue_3 = Box.createHorizontalGlue();
		horizontalBox_3.add(horizontalGlue_3);
		
		button = new JButton("-");
		button.addActionListener(new ActionListener() {
			/**
			 * Removes one point from pilot points and adds it back to the skill point allowance
			 */
			public void actionPerformed(ActionEvent e) {
				if(pilotPoints > 0) {
					pilotPoints--;
					skillPoints++;
					pilotPointsLabel.setText("" + pilotPoints);
					skillPointLabels.setText("" + skillPoints);
				}
				if(skillPoints != 0) btnMakeCharacter.setEnabled(false);
			}
		});
		horizontalBox_3.add(button);
		
		pilotPointsLabel = new JLabel("" + pilotPoints);
		horizontalBox_3.add(pilotPointsLabel);
		
		button_1 = new JButton("+");
		button_1.addActionListener(new ActionListener() {
			/**
			 * Removes one skill point from the available points and adds it to pilot points
			 */
			public void actionPerformed(ActionEvent e) {
				if(skillPoints > 0) {
					pilotPoints++;
					skillPoints--;
					pilotPointsLabel.setText("" + pilotPoints);
					skillPointLabels.setText("" + skillPoints);
				}
				if(skillPoints == 0 && nameField.getText().isEmpty() == false) btnMakeCharacter.setEnabled(true);
			}
		});
		horizontalBox_3.add(button_1);
		
		rigidArea = Box.createRigidArea(new Dimension(200, 20));
		horizontalBox_3.add(rigidArea);
		
		horizontalBox_7 = Box.createHorizontalBox();
		verticalBox.add(horizontalBox_7);
		
		lblFighter = new JLabel("Fighter");
		horizontalBox_7.add(lblFighter);
		
		horizontalGlue_5 = Box.createHorizontalGlue();
		horizontalBox_7.add(horizontalGlue_5);
		
		button_6 = new JButton("-");
		button_6.addActionListener(new ActionListener() {
			/**
			 * Removes one point from fighter points and adds it back to the skill point allowance
			 */
			public void actionPerformed(ActionEvent e) {
				if(fighterPoints > 0) {
					fighterPoints--;
					skillPoints++;
					fighterPointsLabel.setText("" + fighterPoints);
					skillPointLabels.setText("" + skillPoints);
				}
				if(skillPoints != 0) btnMakeCharacter.setEnabled(false);
			}
		});
		horizontalBox_7.add(button_6);
		
		fighterPointsLabel = new JLabel("" + fighterPoints);
		horizontalBox_7.add(fighterPointsLabel);
		
		button_7 = new JButton("+");
		button_7.addActionListener(new ActionListener() {
			/**
			 * Removes one skill point from the available points and adds it to fighter points
			 */
			public void actionPerformed(ActionEvent e) {
				if(skillPoints > 0) {
					fighterPoints++;
					skillPoints--;
					fighterPointsLabel.setText("" + fighterPoints);
					skillPointLabels.setText("" + skillPoints);
				}
				if(skillPoints == 0 && nameField.getText().isEmpty() == false) btnMakeCharacter.setEnabled(true);
			}
		});
		horizontalBox_7.add(button_7);
		
		rigidArea_1 = Box.createRigidArea(new Dimension(200, 20));
		horizontalBox_7.add(rigidArea_1);
		
		horizontalBox_6 = Box.createHorizontalBox();
		verticalBox.add(horizontalBox_6);
		
		lblTrader = new JLabel("Trader");
		horizontalBox_6.add(lblTrader);
		
		horizontalGlue_6 = Box.createHorizontalGlue();
		horizontalBox_6.add(horizontalGlue_6);
		
		button_4 = new JButton("-");
		button_4.addActionListener(new ActionListener() {
			/**
			 * Removes one point from trader points and adds it back to the skill point allowance
			 */
			public void actionPerformed(ActionEvent e) {
				if(traderPoints > 0) {
					traderPoints--;
					skillPoints++;
					traderPointsLabel.setText("" + traderPoints);
					skillPointLabels.setText("" + skillPoints);
				}
				if(skillPoints != 0) btnMakeCharacter.setEnabled(false);
			}
		});
		horizontalBox_6.add(button_4);
		
		traderPointsLabel = new JLabel("" + traderPoints);
		horizontalBox_6.add(traderPointsLabel);
		
		button_5 = new JButton("+");
		button_5.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				/**
				 * Removes one skill point from the available points and adds it to trader points
				 */
				if(skillPoints > 0) {
					traderPoints++;
					skillPoints--;
					traderPointsLabel.setText("" + traderPoints);
					skillPointLabels.setText("" + skillPoints);
				}
				if(skillPoints == 0 && nameField.getText().isEmpty() == false) btnMakeCharacter.setEnabled(true);
			}
		});
		horizontalBox_6.add(button_5);
		
		rigidArea_2 = Box.createRigidArea(new Dimension(200, 20));
		horizontalBox_6.add(rigidArea_2);
		
		horizontalBox_5 = Box.createHorizontalBox();
		verticalBox.add(horizontalBox_5);
		
		lblEngineer = new JLabel("Engineer");
		horizontalBox_5.add(lblEngineer);
		
		horizontalGlue_7 = Box.createHorizontalGlue();
		horizontalBox_5.add(horizontalGlue_7);
		
		button_2 = new JButton("-");
		button_2.addActionListener(new ActionListener() {
			/**
			 * Removes one point from engineer points and adds it back to the skill point allowance
			 */
			public void actionPerformed(ActionEvent e) {
				if(engineerPoints > 0) {
					engineerPoints--;
					skillPoints++;
					engineerPointsLabel.setText("" + engineerPoints);
					skillPointLabels.setText("" + skillPoints);
				}
				if(skillPoints != 0) btnMakeCharacter.setEnabled(false);
			}
		});
		horizontalBox_5.add(button_2);
		
		engineerPointsLabel = new JLabel("" + engineerPoints);
		horizontalBox_5.add(engineerPointsLabel);
		
		button_3 = new JButton("+");
		button_3.addActionListener(new ActionListener() {
			/**
			 * Removes one skill point from the available points and adds it to engineer points
			 */
			public void actionPerformed(ActionEvent e) {
				if(skillPoints > 0) {
					engineerPoints++;
					skillPoints--;
					engineerPointsLabel.setText("" + engineerPoints);
					skillPointLabels.setText("" + skillPoints);
				}
				if(skillPoints == 0 && nameField.getText().isEmpty() == false) btnMakeCharacter.setEnabled(true);
			}
		});
		horizontalBox_5.add(button_3);
		
		rigidArea_3 = Box.createRigidArea(new Dimension(200, 20));
		horizontalBox_5.add(rigidArea_3);
		
		btnMakeCharacter = new JButton("Make Character");
		btnMakeCharacter.addActionListener(new ActionListener() {
			/**
			 * Verifies that a name has been entered, a difficulty has been selected, and all skill
			 * points have been assigned, and then creates a new Player object using that data.
			 */
			public void actionPerformed(ActionEvent e) {
				boolean hasName = !nameField.getText().isEmpty();
				boolean hasPoints = skillPoints == 0;
				boolean hasDifficulty = (rdbtnBeginner.isSelected() || rdbtnEasy.isSelected() || rdbtnHard.isSelected() 
						|| rdbtnImpossible.isSelected() || rdbtnNormal.isSelected());
				if(hasName && hasPoints && hasDifficulty) {
					player = new Player(nameField.getText(), engineerPoints, fighterPoints, pilotPoints, traderPoints, 1000);
					gui.setPlayer(player);
					gui.updateSolarSystem();
					gui.updateMarketPlace(player);
					gui.getChartPanel().setCurrSystem(gui.getCurrentSystem());
					gui.updateTargetSystem(null);
					gui.getChartPanel().repaint();
					frame.dispose();
				} else {
					String message = "";
					if(!hasName) message = message + "Please enter your name.";
					if(!hasPoints) message = message + "\nPlease verify that you have spent your skill points.";
					if(!hasDifficulty) message = message + "\nPlease select a difficulty";
					JOptionPane.showMessageDialog(panel, message);
				}
			}
		});
		btnMakeCharacter.setEnabled(false);
		frame.getContentPane().add(btnMakeCharacter);
	}

}
